<?php
if( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 订单model层
 * @author Toby.Tu 2016-04-19
 */
class User_model extends My_Model{
    public $tab = 'user';
	public $pk = 'id';
    /**
     * 获取用户列表
     * @author Toby.tu 2016-09-01
     */
    public function getUserList($search=array(),$current_page=1,$pagesize=0){
        $field = 'user.*,group.name as group_name,permis.name as permis_name,
                    brand.name as brand_name';
        $query = $this->select($field)
                    ->join('group','group.id=user.group_id','left')
                    ->join('permis','permis.id=user.permis_id','left')
                    ->join('brand','brand.id=user.brand_id','left')
                    ->where('user.deleted',0);
        if(isset($search['name'])){//用户名
            $query->like('user.name',$search['name']);
        }
        if(isset($search['user_name'])){//姓名
            $query->like('user.user_name',$search['user_name']);
        }
        if(isset($search['permis_id'])){//权限组
            $query->where('user.permis_id',$search['permis_id']);
        }
        if(isset($search['group_id'])){//部门
            $query->where('user.group_id',$search['group_id']);
        }
        if(isset($search['brand_id'])){//品牌
            $query->where('user.brand_id',$search['brand_id']);
        }
        if(isset($search['status'])){//状态
            $query->where('user.status',$search['status']);
        }
        $query->order_by('user.id');
        if(empty($pagesize)){
            $pagesize = $this->pagesize;
        }
        $start = ($current_page-1)*$pagesize;
        $query->limit($pagesize,$start);
        $rows = $this->get();
        if(!empty($rows)){
            return $rows;
        }
        return array();
    }
    /**
     * 获取用户总数
     * @author Toby.tu 2016-09-01
     */
    public function getUserListCount($search=array()){
        $tab = $this->getTabName();
        $field = 'count('.$tab.'.id) as user_count';
        $query = $this->select($field)
                    ->join('group','group.id=user.group_id','left')
                    ->join('permis','permis.id=user.permis_id','left')
                    ->join('brand','brand.id=user.brand_id','left')
                    ->where('user.deleted',0);
        if(isset($search['name'])){//用户名
            $query->like('user.name',$search['name']);
        }
        if(isset($search['user_name'])){//姓名
            $query->like('user.user_name',$search['user_name']);
        }
        if(isset($search['permis_id'])){//权限组
            $query->where('user.permis_id',$search['permis_id']);
        }
        if(isset($search['group_id'])){//部门
            $query->where('user.group_id',$search['group_id']);
        }
        if(isset($search['brand_id'])){//品牌
            $query->where('user.brand_id',$search['brand_id']);
        }
        if(isset($search['status'])){//状态
            $query->where('user.status',$search['status']);
        }
        $query->order_by('user.id');
        $row = $this->getRow();
        if(!empty($row)){
            return $row['user_count'];
        }
        return 0;
    }
    /**
     * 加密密码
     * @author Toby.tu 2017-03-01
     */
    public function encodePasseord($pwd=''){
        if(empty($pwd)) return '';
        //调用两次加密，如果未来有模拟登录的需求，直接传递一次加密的密码值即可
        return $this->common->pwd($this->common->pwd('user_encodepasseord_'.$pwd));
    }
}
